<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="设备id" prop="dvId">
        <el-input
          v-model="queryParams.dvId"
          placeholder="请输入设备id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="故障日期" prop="troubleDate">
        <el-date-picker clearable
          v-model="queryParams.troubleDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择故障日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="设备名称" prop="dvName">
        <el-input
          v-model="queryParams.dvName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备编码" prop="dvCode">
        <el-input
          v-model="queryParams.dvCode"
          placeholder="请输入设备编码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检修负责人" prop="overhaulPerson">
        <el-input
          v-model="queryParams.overhaulPerson"
          placeholder="请输入检修负责人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报修时间" prop="repairsDate">
        <el-date-picker clearable
          v-model="queryParams.repairsDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择报修时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="使用部门" prop="usePart">
        <el-input
          v-model="queryParams.usePart"
          placeholder="请输入使用部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="维修人" prop="fixPerson">
        <el-input
          v-model="queryParams.fixPerson"
          placeholder="请输入维修人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修理时间" prop="fixDate">
        <el-date-picker clearable
          v-model="queryParams.fixDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择修理时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修理完工时间" prop="fixEndDate">
        <el-date-picker clearable
          v-model="queryParams.fixEndDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择修理完工时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="故障部位" prop="troubleBw">
        <el-input
          v-model="queryParams.troubleBw"
          placeholder="请输入故障部位"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="配件更换" prop="pjChange">
        <el-input
          v-model="queryParams.pjChange"
          placeholder="请输入配件更换"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="排序" prop="sort">
        <el-input
          v-model="queryParams.sort"
          placeholder="请输入排序"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['base:repair:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['base:repair:edit']"
        >修改</el-button> -->
      <!-- </el-col> -->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['base:repair:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['base:repair:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="repairList" @selection-change="handleSelectionChange">
      <el-table-column label="操作" width="66" align="center">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['base:repair:edit']"
          >修改</el-button> -->
              <el-button
            size="text"
            type="primary"
            icon="el-icon-edit"
            @click="dispatchTask(scope.row)"
            v-hasPermi="['base:repair:edit']"
          >处理</el-button>
          
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['base:repair:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="id
" align="center" prop="id" /> -->
      <!-- <el-table-column label="设备id" align="center" prop="dvId" /> -->
      <el-table-column label="故障日期" align="center" prop="troubleDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.troubleDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="设备名称" align="center" prop="dvName" />
      <el-table-column label="设备编码" align="center" prop="dvCode" />
      <el-table-column label="检修负责人" align="center" prop="overhaulPerson" />
      <el-table-column label="型号规格" align="center" prop="cvType" />
      <el-table-column label="报修时间" align="center" prop="repairsDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.repairsDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="使用部门" align="center" prop="usePart" />
      <el-table-column label="维修人" align="center" prop="fixPerson" />
      <el-table-column label="修理时间" align="center" prop="fixDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fixDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="修理完工时间" align="center" prop="fixEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.fixEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="故障照片" align="center" prop="troubleImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.troubleImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="故障现象" align="center" prop="troubleXh" />
      <el-table-column label="故障原因" align="center" prop="troubleWhy" />
      <el-table-column label="故障部位" align="center" prop="troubleBw" />
      <el-table-column label="配件更换" align="center" prop="pjChange" />
      <el-table-column label="故障处理结果" align="center" prop="troubleResult" />
      <el-table-column label="完成照片" align="center" prop="completeImg" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.completeImg" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="备注" align="center" prop="remark" />
    
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 添加或修改报修管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="设备id" prop="dvId">
          <el-input v-model="form.dvId" placeholder="请输入设备id" />
        </el-form-item> -->
        <el-form-item label="故障日期" prop="troubleDate">
          <el-date-picker clearable
            v-model="form.troubleDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择故障日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="设备名称" prop="dvName">
          <el-input v-model="form.dvName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="设备编码" prop="dvCode">
          <el-input v-model="form.dvCode" placeholder="请输入设备编码" />
        </el-form-item>
        <el-form-item label="检修负责人" prop="overhaulPerson">
          <el-input v-model="form.overhaulPerson" placeholder="请输入检修负责人" />
        </el-form-item>
        <el-form-item label="报修时间" prop="repairsDate">
          <el-date-picker clearable
            v-model="form.repairsDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择报修时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="使用部门" prop="usePart">
          <el-input v-model="form.usePart" placeholder="请输入使用部门" />
        </el-form-item>
        
        <el-form-item label="维修人" prop="fixPerson">
          <el-select
            v-model="form.fixPerson"
            @change="changefixPerson"
            placeholder="请选择"
          >
            <el-option
              v-for="item in fixPersons"
              :key="item.userId"
              :label="item.username"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="修理时间" prop="fixDate">
          <el-date-picker clearable
            v-model="form.fixDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择修理时间">
          </el-date-picker>
        </el-form-item>
        <!-- <el-form-item label="修理完工时间" prop="fixEndDate">
          <el-date-picker clearable
            v-model="form.fixEndDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择修理完工时间">
          </el-date-picker>
        </el-form-item> -->
        <el-form-item label="故障照片" prop="troubleImg">
          <image-upload v-model="form.troubleImg"/>
        </el-form-item>
        <el-form-item label="故障现象" prop="troubleXh">
          <el-input v-model="form.troubleXh" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="故障原因" prop="troubleWhy">
          <el-input v-model="form.troubleWhy" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <!-- <el-form-item label="故障部位" prop="troubleBw">
          <el-input v-model="form.troubleBw" placeholder="请输入故障部位" />
        </el-form-item>
        <el-form-item label="配件更换" prop="pjChange">
          <el-input v-model="form.pjChange" placeholder="请输入配件更换" />
        </el-form-item>
        <el-form-item label="故障处理结果" prop="troubleResult">
          <el-input v-model="form.troubleResult" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="完成照片" prop="completeImg">
          <image-upload v-model="form.completeImg"/>
        </el-form-item> -->
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listRepair, getRepair, delRepair, addRepair, updateRepair,getFixPersons } from "@/api/base/repair";

export default {
  name: "Repair",
  data() {
    return {
      //维修人列表
      fixPersons:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 报修管理表格数据
      repairList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        dvId: null,
        troubleDate: null,
        dvName: null,
        dvCode: null,
        overhaulPerson: null,
        cvType: null,
        repairsDate: null,
        usePart: null,
        fixPerson: null,
        fixDate: null,
        fixEndDate: null,
        troubleImg: null,
        troubleXh: null,
        troubleWhy: null,
        troubleBw: null,
        pjChange: null,
        troubleResult: null,
        completeImg: null,
        sort: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getFixPersonList();
  },
  methods: {
    /** 查询报修管理列表 */
    getList() {
      this.loading = true;
      listRepair(this.queryParams).then(response => {
        this.repairList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    dispatchTask(row){
      this.reset();
      const id = row.id || this.ids
      getRepair(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报修管理";
      });
    },
    //获取维修人列表
    getFixPersonList(){
      // 获取维修人列表
      getFixPersons().then(response => {
        this.fixPersons = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        updateTime: null,
        updateBy: null,
        createBy: null,
        createTime: null,
        dvId: null,
        troubleDate: null,
        dvName: null,
        dvCode: null,
        overhaulPerson: null,
        cvType: null,
        repairsDate: null,
        usePart: null,
        fixPerson: null,
        fixDate: null,
        fixEndDate: null,
        troubleImg: null,
        troubleXh: null,
        troubleWhy: null,
        troubleBw: null,
        pjChange: null,
        troubleResult: null,
        completeImg: null,
        sort: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    changefixPerson(person) {
      this.form.fixPerson=person;
      this.form.fixPersonId=person
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加报修管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getRepair(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改报修管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRepair(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRepair(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除报修管理编号为"' + ids + '"的数据项？').then(function() {
        return delRepair(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('base/repair/export', {
        ...this.queryParams
      }, `repair_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style scoped>
.handle-container .el-button{
margin-top: 20px;
}
</style>
